// OverwriteDialog.cpp

#include "StdAfx.h"

#include "Common/String/StringConvert.h"

#include "Common/FileSystem/FileName.h"
#include "Common/System/Defs.h"
#include "Common/String/ResourceString.h"
#include "Common/String/DataConversions.h"

#include "Common/String/FormatUtils.h"
#include "OverwriteDialog.h"

#include "Common/MyFixes.h" //dummy char

#ifdef LANG
#include "Common/Lang/LangUtils.h"
#endif

using namespace Common;

static const int kCurrentFileNameSizeLimit = 82;

namespace NOverwriteDialog {
	LPCTSTR CFileInfo::GetFileSize()
	{
		if (SizeIsDefined)
		{
			CString fileSize;			

			if(NULL != ::StrFormatByteSize(Size, fileSize.GetBuffer(MAX_PATH), MAX_PATH))
			{
				return fileSize;
			}
		}
		return NULL;
	}

	LPCTSTR CFileInfo::GetFileDate()
	{
		CString result;
		if (TimeIsDefined)
		{
			FILETIME localFileTime;
			if (!FileTimeToLocalFileTime(&Time, &localFileTime))
			  throw 4190402;
			result =  String::ConvertFileTimeToString(localFileTime);
			return result;
		}
		return NULL;
	}

	CString CFileInfo::GetDescription()
	{
		CString fullString;

		CString sizeString;
		  if (SizeIsDefined)
			sizeString = String::PatternFormat(IDS_FILE_SIZE,
				#ifdef LANG
				0x02000982,
				#endif
				String::NumberToString(Size));

		  const CString &fileName = Name;
		  int slashPos = fileName.ReverseFind(CHAR_PATH_SEPARATOR);
		  CString s1, s2;
		  if (slashPos >= 0)
		  {
			s1 = fileName.Left(slashPos + 1);
			s2 = fileName.Mid(slashPos + 1);
		  }
		  else
			s2 = fileName;
		  ReduceString(s1);
		  ReduceString(s2);
  
		  fullString = s1 + _T('\n') + s2;
		  fullString += _T('\n');
		  fullString += sizeString;
		  fullString += _T('\n');

		  if (TimeIsDefined)
		  {
			CString timeString;
			FILETIME localFileTime;
			if (!FileTimeToLocalFileTime(&Time, &localFileTime))
			  throw 4190402;
			timeString =  String::ConvertFileTimeToString(localFileTime);

			fullString +=
			#ifdef LANG
			Lang::LangString(IDS_FILE_MODIFIED, 0x02000983);
			#else
			MyLoadString(IDS_FILE_MODIFIED);
			#endif

			fullString += _T(" ");
			fullString += timeString;
		  }
		  return fullString;
	}
	HICON CFileInfo::GetIcon()
	{
		SHFILEINFO shellFileInfo;
		if (::SHGetFileInfo(String::GetSystemString(Name), FILE_ATTRIBUTE_NORMAL, &shellFileInfo,
		sizeof(shellFileInfo), SHGFI_ICON | SHGFI_USEFILEATTRIBUTES | SHGFI_LARGEICON))
		{
			return shellFileInfo.hIcon;
		}
		return NULL;
	}
	void CFileInfo::ReduceString(CString &s)
	{
	  int size = kCurrentFileNameSizeLimit;
	  if (s.GetLength() > size)
		s = s.Left(size / 2) + CString(_T(" ... ")) + s.Right(size / 2);
	}
}

#ifdef LANG
static Lang::CIDLangPair kIDLangPairs[] =
{
  { IDC_STATIC_OVERWRITE_HEADER,         0x02000901},
  { IDC_STATIC_OVERWRITE_QUESTION_BEGIN, 0x02000902 },
  { IDC_STATIC_OVERWRITE_QUESTION_END,   0x02000903 },
  { IDYES, 0x02000705 },
  { IDC_BUTTON_OVERWRITE_YES_TO_ALL, 0x02000707 },
  { IDNO,  0x02000709 },  
  { IDCANCEL, 0x02000711 }
};
#endif





LRESULT COverwriteDialog::OnInitDialog(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/)
{
  #ifdef LANG
  Lang::LangSetWindowText(HWND(*this), 0x02000900);
  Lang::LangSetDlgItemsText(HWND(*this), kIDLangPairs, sizeof(kIDLangPairs) / sizeof(kIDLangPairs[0]));
  #endif
  CenterWindow(GetParent());
  DoDataExchange(FALSE);
  m_strSrcFileDate = OldFileInfo.GetFileDate();
  if (!m_strSrcFileDate.IsEmpty())
  {
	  m_strSrcFileDate = String::PatternFormat(IDS_FILE_MODIFIED, 0x03020A04, m_strSrcFileDate);
  }
  m_strDstFileDate = NewFileInfo.GetFileDate();
  if (!m_strDstFileDate.IsEmpty())
  {
	  m_strDstFileDate = String::PatternFormat(IDS_FILE_MODIFIED, 0x03020A04, m_strDstFileDate);
  }
  m_strSrcFileSize = OldFileInfo.GetFileSize();
  if(!m_strSrcFileSize.IsEmpty())
  {
	  m_strSrcFileSize = String::PatternFormat(IDS_FILE_SIZE, 0x03020A04, m_strSrcFileSize);
  }
  m_strDstFileSize = NewFileInfo.GetFileSize();
  if(!m_strDstFileSize.IsEmpty())
  {
	  m_strDstFileSize = String::PatternFormat(IDS_FILE_SIZE, 0x03020A04, m_strDstFileSize);
  }
  HICON hIcon = NULL;
  hIcon = NewFileInfo.GetIcon();
  if(NULL != hIcon)
	m_wNewControl.SetIcon(hIcon);
  hIcon = OldFileInfo.GetIcon();
  if(NULL != hIcon)
	m_wOldControl.SetIcon(hIcon);
  DoDataExchange(FALSE);
  DoDataExchange(TRUE);

  return TRUE;
}

LRESULT COverwriteDialog::OnButtonCmd(WORD /*wNotifyCode*/, WORD wID, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
{
  EndDialog(wID);
  return TRUE;  
}
